/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.edu.mobileWeb.course;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.idenger.modules.edu.entity.common.BaseResponseMsg;
import com.idenger.modules.edu.entity.common.ResponseMsg;
import com.idenger.modules.edu.entity.course.CourseVideoAndPracticeDto;
import com.idenger.modules.edu.entity.course.EduCourse;
import com.idenger.modules.edu.entity.course.EduSection;
import com.idenger.modules.edu.entity.course.EduVideoHistory;
import com.idenger.modules.edu.service.practice.EduPracticeService;
import com.idenger.modules.edu.web.comm.Constant;
import com.idenger.modules.edu.entity.course.*;
import com.idenger.modules.edu.entity.enroll.EduEnroll;
import com.idenger.modules.edu.entity.exercisehistory.EduExerciseHistory;
import com.idenger.modules.edu.entity.user.EduUser;
import com.idenger.modules.edu.service.course.EduCourseContentService;
import com.idenger.modules.edu.service.course.EduSectionService;
import com.idenger.modules.edu.service.course.EduVideoHistoryService;
import com.idenger.modules.edu.service.enroll.EduEnrollService;
import com.idenger.modules.edu.service.exercisehistory.EduExerciseHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.idenger.common.persistence.Page;
import com.idenger.common.utils.StringUtils;
import com.idenger.common.web.BaseController;
import com.idenger.modules.edu.entity.teacher.EduTeacherInfo;
import com.idenger.modules.edu.service.course.EduCourseService;
import com.idenger.modules.edu.service.teacher.EduTeacherInfoService;


/**
 * 课程Controller
 * @author chenlin
 * @version 2017-11-29
 */
@Controller
@RequestMapping(value = "${mobilePath}/edu/course/eduCourse")
public class MEduCourseController extends BaseController {

	@Autowired
	private EduCourseService eduCourseService;
	
	@Autowired
	private EduTeacherInfoService eduTeacherInfoService;

	@Autowired
	private EduSectionService eduSectionService;

	@Autowired
	private EduCourseContentService eduCourseContentService;

	@Autowired
	private EduPracticeService eduPracticeService;

	@Autowired
	private EduVideoHistoryService eduVideoHistoryService;

	@Autowired
	private EduExerciseHistoryService eduExerciseHistoryService;
	
	@Autowired
	private EduEnrollService eduEnrollService;

	@ModelAttribute
	public EduCourse get(@RequestParam(required=false) String id) {
		EduCourse entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eduCourseService.get(id);
		}
		if (entity == null){
			entity = new EduCourse();                                 
		}
		return entity;
	}
	

	//课程列表
	@RequestMapping(value = "courseList")
	public String courseList(EduCourse eduCourse, HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		eduCourse.setOpen(Constant.COURSE_OPEN_YES);
		eduCourse.setStatus(Constant.COURSE_STATUS_YES);
		eduCourse.setStudyEndTime(new Date());
		Page<EduCourse> page = eduCourseService.findPage(new Page<EduCourse>(request, response), eduCourse); 
		if(page.getList().size()>0){
			for(int i =0; i<page.getList().size();i++){
				EduCourse eduCour = page.getList().get(i);
				List<EduTeacherInfo> eduTeacher =  eduTeacherInfoService.findCourseTeacherDetailByCourseId(eduCour.getId());
				if(eduTeacher!=null && eduTeacher.size()>0){
					page.getList().get(i).setEduTeacher(eduTeacher);
				}
			}
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
			baseResponseMsg.addData("page",page);
		}else{
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
			baseResponseMsg.addData("None");
		}
		return renderString(response, baseResponseMsg);
	}

	//查找课程详情
	@RequestMapping(value = "findcourseDetailById")
	public String findcourseDetailById(EduCourse eduCourse, HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		if(eduCourse!=null){
			//课程下的老师
			List<EduTeacherInfo> teacherList =eduTeacherInfoService.findCourseTeacherDetailByCourseId(eduCourse.getId());
			//课程下的练习跟视频
			List<EduSection> eduSectionList = eduSectionService.findEduSectionByCourseId(eduCourse.getId());
			List<EduSection> eduSections = new ArrayList<>();
			for (EduSection educontentSetion : eduSectionList) {
				List<CourseVideoAndPracticeDto>	eduCourseContentList=eduCourseContentService.findEduCourseContentBysectionId(educontentSetion.getId());
				List<CourseVideoAndPracticeDto> eduCourseContentList2 = eduPracticeService.findEduPracticeBysectionId(educontentSetion.getId());
				educontentSetion.setCourseVideoDtoList(eduCourseContentList);
				educontentSetion.setCoursePracticeDtoList(eduCourseContentList2);
				eduSections.add(educontentSetion);
			}
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
			Map<String, Object> map = new HashMap<>();
			map.put("courseDetail",eduCourse);
			map.put("teacherList",teacherList);
			map.put("eduSections",eduSections);
			//订单的状态
			baseResponseMsg.addData(map);
		}else{
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
			baseResponseMsg.addData("没有数据,该课程有问题");
		}
		return renderString(response, baseResponseMsg);
	}
	/***
	 * 是否购买
	 * @param userId
	 * @param courseId
	 * @return
	 */
	public String whethPayCourse(String userId,String courseId){
		EduEnroll eduEnroll =  eduEnrollService.findwhethPayCourse(courseId, userId);
		if(eduEnroll == null){
			return "0";
		}
		return "1";
	}
	


	//查询我的课程详情
	@RequestMapping(value = "findcourseDetailByIdAndUserId")
	public String findcourseDetailByIdAndUserId(EduCourse eduCourse, HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		//获取当前登录者
		EduUser eduUser = (EduUser) request.getAttribute("currentUser");
		if(eduCourse!=null){
			//课程下的老师
			List<EduTeacherInfo> teacherList =eduTeacherInfoService.findCourseTeacherDetailByCourseId(eduCourse.getId());
			//课程下的练习跟视频
			List<EduSection> eduSectionList = eduSectionService.findEduSectionByCourseId(eduCourse.getId());
			List<EduSection> eduSections = new ArrayList<>();
			for (EduSection educontentSetion : eduSectionList) {
				List<CourseVideoAndPracticeDto>	eduCourseContentList=eduCourseContentService.findEduCourseContentBysectionId(educontentSetion.getId());
				//视频
				video(eduCourseContentList,eduUser);
				List<CourseVideoAndPracticeDto> eduCourseContentList2 = eduPracticeService.findEduPracticeBysectionId(educontentSetion.getId());
				//练习
				practice(eduCourseContentList2,eduUser);
				educontentSetion.setCourseVideoDtoList(eduCourseContentList);
				educontentSetion.setCoursePracticeDtoList(eduCourseContentList2);
				eduSections.add(educontentSetion);
			}
			String payStatus = whethPayCourse(eduUser.getId(), eduCourse.getId());

			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
			Map<String, Object> map = new HashMap<>();
			map.put("courseDetail",eduCourse);
			map.put("teacherList",teacherList);
			map.put("eduSections",eduSections);
			//订单的状态
			map.put("payStatus",payStatus);
			baseResponseMsg.addData(map);
		}else{
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
			baseResponseMsg.addData("没有数据,该课程有问题");
		}
		return renderString(response, baseResponseMsg);
	}

	public void video(List<CourseVideoAndPracticeDto>	eduCourseContentList,EduUser eduUser){
		for(int i=0;i<eduCourseContentList.size();i++){
			//视频列表
			CourseVideoAndPracticeDto courseVideoAndPracticeDto = eduCourseContentList.get(i);
			//根据视频ID查询视频历史记录
			EduVideoHistory eduVideoHistory = new EduVideoHistory();
			eduVideoHistory.setCourseContentId(courseVideoAndPracticeDto.getId());
			eduVideoHistory.setUserId(eduUser.getId());//用户 ID
			List<EduVideoHistory> listVideo = eduVideoHistoryService.findList(eduVideoHistory);
			if(listVideo.size()>0){
				//已经看过
				courseVideoAndPracticeDto.setStatus(Constant.VIDEO_STATUS_YES);
			}else{
				//买了还没看
				courseVideoAndPracticeDto.setStatus(Constant.VIDEO_STATUS_NO);
			}
		}
	}
	public void practice(List<CourseVideoAndPracticeDto> eduCourseContentList2,EduUser eduUser){
		for(int i =0;i<eduCourseContentList2.size();i++){
			CourseVideoAndPracticeDto courseVideoAndPracticeDto = eduCourseContentList2.get(i);
			//查询练习做题记录
			EduExerciseHistory eduExerciseHistory = new EduExerciseHistory();
			eduExerciseHistory.setUserId(eduUser.getId());
			eduExerciseHistory.setPracticeId(courseVideoAndPracticeDto.getId());
			EduExerciseHistory elist =eduExerciseHistoryService.findDoPracticeMaxScoreByUserIdAndPracticeId(eduExerciseHistory);
			if(elist!=null){
				courseVideoAndPracticeDto.setEduExerciseHistory(elist);
			}else{
				courseVideoAndPracticeDto.setEduExerciseHistory(new EduExerciseHistory());
			}
		}
	}

	
	
}